/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.service.impl;

import com.workcell.workdata.im.component.redis.OrganizationRedisTemplate;
import com.workcell.workdata.im.entity.Organization;
import com.workcell.workdata.im.entity.User;
import com.workcell.workdata.im.model.vo.OrganizationVO;
import com.workcell.workdata.im.repository.DepartmentMemberRepository;
import com.workcell.workdata.im.repository.DepartmentRepository;
import com.workcell.workdata.im.repository.OrganizationRepository;
import com.workcell.workdata.im.repository.UserRepository;
import com.workcell.workdata.im.service.OrganizationService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {

	@Resource
	private OrganizationRepository organizationRepository;

	@Resource
	private UserRepository userRepository;

	@Resource
	private OrganizationRedisTemplate organizationRedisTemplate;

	@Resource
	private DepartmentRepository departmentRepository;

	@Resource
	private DepartmentMemberRepository departmentMemberRepository;

	@Override
	public OrganizationVO findOne(long id) {

		Organization organization = organizationRepository.findById(id).orElse(null);

		if (organization == null){
			return null;
		}

		OrganizationVO vo = OrganizationVO.of(organization);

		vo.setDepartmentList(departmentRepository.findList(id));

		vo.setMemberList(departmentMemberRepository.findList(id));

		return vo;
	}

	@Override
	public List<User> findList(long id) {
		return userRepository.findList(id);
	}

	@Override
	public List<Long> findUidListX(long uid) {
		Long organizationId = userRepository.getOrganizationId(uid);
		if (organizationId == null){
			return new LinkedList<>();
		}
		return findUidList(organizationId);
	}

	@Override
	public List<Long> findUidList(long id) {
		List<Long> cachedUidList = organizationRedisTemplate.findUidList(id);
		if (!CollectionUtils.isEmpty(cachedUidList)){
			return cachedUidList;
		}

		List<Long> uidList = userRepository.findIdList(id);
		if (!CollectionUtils.isEmpty(uidList)){
			organizationRedisTemplate.add(id,uidList);
		}

		return uidList;
	}
}
